package com.cskaoyan.service.admin;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.topic.ShowTopicInfo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author by yxy_8175@163.com
 * @Description TODO
 * @Date 2022-11-20 14:56
 */

@Service
public class TopicServiceImpl implements TopicService{

    @Autowired
    MarketTopicMapper topicMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;


    @Override
    public CommonData<MarketTopic> selectByPage(BasePageInfo info, String title, String subtitle) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();

        if (title != null){
            criteria.andTitleLike("%"+title+"%");
        }
        if (subtitle != null){
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
        if (info.getSort() != null || info.getOrder() != null){
            example.setOrderByClause(info.getSort()+" "+info.getOrder());
        }

        // 逻辑删除开关
        criteria.andDeletedEqualTo(false);

        // List<MarketCoupon> list =  couponMapper.selectAll();
        List<MarketTopic> list = topicMapper.selectByExample(example);

        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);

        return CommonData.data(pageInfo);
    }

    @Transactional
    @Override
    public MarketTopic createTopic(MarketTopic topic) {
        // 添加 增加时间和更新时间
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());

        topicMapper.insertSelective(topic);

        Integer id = topic.getId();

        return topicMapper.selectByPrimaryKey(id);

    }

    @Transactional
    @Override
    public MarketTopic updateTopicInfo(MarketTopic topic) {
        topic.setUpdateTime(new Date());
        topicMapper.updateByPrimaryKeySelective(topic);
        return topicMapper.selectByPrimaryKey(topic.getId());
    }

    @Override
    public ShowTopicInfo selectTopicById(Integer id) {
        ShowTopicInfo showTopicInfo = new ShowTopicInfo();
        // 获取主题信息
        MarketTopic topic = topicMapper.selectByPrimaryKey(id);
        showTopicInfo.setTopic(topic);

        String[] goods = topic.getGoods();
        ArrayList<MarketGoods> goodList = new ArrayList<>();
        for (String good : goods) {
            // 查询商品id
            int goodId = Integer.parseInt(good);
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodId);
            goodList.add(marketGoods);
        }
        showTopicInfo.setGoodsList(goodList);

        return showTopicInfo;
    }

    @Override
    public int deleteTopic(MarketTopic topic) {

        // 直接修改delete字段  逻辑删除
        topic.setDeleted(true);
        int i = topicMapper.updateByPrimaryKeySelective(topic);
        // 实际删除
        // int i = couponMapper.deleteByPrimaryKey(id);
        if (i > 0){
            return BaseRespVo.STATUS_OK;
        }
        return BaseRespVo.STATUS_DELETE_FAILED;
    }

    @Transactional
    @Override
    public void deleteTopicMultiple(Integer[] ids) {
        // for (inte id : ids) {
        //     MarketTopic topic = topicMapper.selectByPrimaryKey(Integer.parseInt(id));
        //     topic.setDeleted(true);
        //     topicMapper.updateByPrimaryKey(topic);
        // }
        for (Integer id : ids) {
            MarketTopic topic = topicMapper.selectByPrimaryKey(id);
            topic.setDeleted(true);
            topicMapper.updateByPrimaryKey(topic);
        }
    }

    @Override
    public CommonData<MarketTopic> queryRelatedList(Integer topicId,BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getLimit());

        MarketTopicExample example = new MarketTopicExample();
        example.or().andIdEqualTo(topicId).andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = topicMapper.selectByExample(example);
        if (marketTopics.size() == 0){
            // todo 若无此文章，则推荐数据库中最新添加的4篇文章
            return query(pageInfo);
        }
        MarketTopic topic = marketTopics.get(0);
        String[] goods = topic.getGoods();

        if (goods.length == 0){
            return query(pageInfo);
        }
        String goodsStr = Arrays.toString(goods);
        String substring = goodsStr.substring(1, goodsStr.length() - 1);
        substring = "%"+substring+"%";
        List<MarketTopic> retends = topicMapper.selectRecommendByGoods(substring);

        if (retends.size() == 0){
            return query(pageInfo);
        }

        PageInfo<MarketTopic> retendPage = new PageInfo<>(retends);
        return CommonData.data(retendPage);
    }

    private CommonData<MarketTopic> query(BasePageInfo pageInfo) {
        PageHelper.startPage(pageInfo.getPage(),pageInfo.getLimit());
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);

        example.setOrderByClause(pageInfo.getSort()+" "+pageInfo.getOrder());

        List<MarketTopic> list = topicMapper.selectByExample(example);
        PageInfo<MarketTopic> page = new PageInfo<>(list);
        return CommonData.data(page);

    }

}
